﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// FeeService 的摘要说明
/// </summary>
public class FeeService
{
    FeeDao feeDao = new FeeDao();
    FeeBean fee = new FeeBean();
    public FeeService()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private FeeBean getFeeBean(DataTable table)
    {
        fee.Id = Convert.ToInt64(SqlHelper.fromDBNull(table.Rows[0]["ps_id"]));
        fee.Name = table.Rows[0]["ps_name"].ToString();
        fee.Pid = Convert.ToInt64(SqlHelper.fromDBNull(table.Rows[0]["ps_pid"]));
        fee.Content = table.Rows[0]["ps_content"].ToString();
        fee.Dest = table.Rows[0]["ps_dest"].ToString();
        fee.CreateTime = Convert.ToDateTime(SqlHelper.fromDBNull(table.Rows[0]["ps_createtime"]));
        fee.CancelTime = Convert.ToDateTime(SqlHelper.fromDBNull(table.Rows[0]["ps_canceltime"]));
        fee.IsDel = Convert.ToBoolean(SqlHelper.fromDBNull(table.Rows[0]["ps_isDel"]));
        fee.Pay = Convert.ToDecimal(SqlHelper.fromDBNull(table.Rows[0]["ps_pay"]));
        return fee;
    }
    private List<FeeBean> getFeeBeanList(DataTable table)
    {
        List<FeeBean> list = new List<FeeBean>();
        FeeBean fee = null;
        for(int i = 0; i < table.Rows.Count; i++)
        {
            fee = new FeeBean();
            fee.Id = Convert.ToInt64(SqlHelper.fromDBNull(table.Rows[i]["ps_id"]));
            fee.Name = table.Rows[i]["ps_name"].ToString();
            fee.Pid = Convert.ToInt64(SqlHelper.fromDBNull(table.Rows[i]["ps_pid"]));
            fee.Content = table.Rows[i]["ps_content"].ToString();
            fee.Dest = table.Rows[i]["ps_dest"].ToString();
            fee.CreateTime = Convert.ToDateTime(SqlHelper.fromDBNull(table.Rows[i]["ps_createtime"]));
            fee.CancelTime = Convert.ToDateTime(SqlHelper.fromDBNull(table.Rows[i]["ps_canceltime"]));
            fee.IsDel = Convert.ToBoolean(SqlHelper.fromDBNull(table.Rows[i]["ps_isDel"]));
            fee.Pay = Convert.ToDecimal(SqlHelper.fromDBNull(table.Rows[i]["ps_pay"]));
            list.Add(fee);
        }
        return list;
    }

    public FeeBean findById(FeeBean fee)
    {
        DataTable table = feeDao.findById(fee.Id);
        FeeBean feeReturn = getFeeBean(table);
        return feeReturn;
    }

    public List<FeeBean> findAll()
    {
        DataTable dt = feeDao.findAll();
        List<FeeBean> treeList = getFeeBeanList(dt);
        return treeList;
    }

    private List<FeeBean> convert2TreeFeeBeans(DataTable table)
    {
        List<FeeBean> nodes = getFeeBeanList(table);
        /*这里设置Node之间的节点关系，遍历设置相互父子关系*/
        
        try
        { for (int i = 0; i < nodes.Count; i++)
            {
                FeeBean n = nodes[i];
                for (int j = i + 1; j < nodes.Count; j++)
                {
                    FeeBean m = nodes[j];
                    if (m.Pid == n.Id)
                    {
                        n.Children.Add(m);
                        m.Parent = n;
                    }
                    else if (n.Pid == m.Id)
                    {
                        m.Children.Add(n);
                        n.Parent = m;
                    }
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return nodes;
    }

    private List<FeeBean> convert2TreeFeeBeansBySort(DataTable table)
    {
        List<FeeBean> list = getFeeBeanList(table);
        List<FeeBean> result = new List<FeeBean>();
        foreach (FeeBean b in list)
        {
            if (b.Pid == 0)
            {
                foreach(FeeBean c in list)
                {
                    if(c.Pid == b.Id)
                    {
                        b.Children.Add(c);
                    }
                }
                result.Add(b);
            }
        }
        return result;
    }
}